﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QualityStormCommon;

namespace QualityStorm {
    public partial class FrmExternalConnections : Form {

        ConnectionContextBag m_contextBag;
        ExternalConnection m_ExternalConnection;

        bool m_ReadOnly = false;

        public bool ReadOnly {
            get {
                return m_ReadOnly;
            }
            set {
                m_ReadOnly = value;
                SetReadOnly();
            }
        }

        private void SetReadOnly() {
            grpType.Enabled = !m_ReadOnly;
            rdoQualityStormServer.Enabled = !m_ReadOnly;
            rdoOPCServer.Enabled = !m_ReadOnly;
            rdoDatabaseServer.Enabled = !m_ReadOnly;
            btnOK.Enabled = !m_ReadOnly;
            btnTest.Enabled = !m_ReadOnly;
            grpQualityStorm.Enabled = !m_ReadOnly;
            lblPassword.Enabled = !m_ReadOnly;
            txtLogin.Enabled = !m_ReadOnly;
            lblLogin.Enabled = !m_ReadOnly;
            txtQualityStormPort.Enabled = !m_ReadOnly;
            lblServerPort.Enabled = !m_ReadOnly;
            txtQualityStormServerName.Enabled = !m_ReadOnly;
            lblQualityStormServerName.Enabled = !m_ReadOnly;
            grpDatabase.Enabled = !m_ReadOnly;
            txtConnectionString.Enabled = !m_ReadOnly;
            lblConnectionString.Enabled = !m_ReadOnly;
            cboClassNames.Enabled = !m_ReadOnly;
            lblClass.Enabled = !m_ReadOnly;
            grpOPC.Enabled = !m_ReadOnly;
            btnSearch.Enabled = !m_ReadOnly;
            txtOPCServerName.Enabled = !m_ReadOnly;
            lblOPCServer.Enabled = !m_ReadOnly;
            txtOpcMachineName.Enabled = !m_ReadOnly;
            lblOPCMachineName.Enabled = !m_ReadOnly;
            lblConnectionName.Enabled = !m_ReadOnly;
            txtConnectionName.Enabled = !m_ReadOnly;
            txtPassword.Enabled = !m_ReadOnly;
        }


        public ExternalConnection ExternalConnection {
            get {
                return m_ExternalConnection;
            }
            set {
                m_ExternalConnection = value;
                ShowData();
            }
        }

        private void ShowData() {
            if (m_ExternalConnection != null) {
                txtConnectionName.Text = m_ExternalConnection.Name;
                grpDatabase.Visible = false;
                grpOPC.Visible = false;
                grpQualityStorm.Visible = false;

                switch (m_ExternalConnection.Type) {
                    case 0:
                        rdoDatabaseServer.Checked = true;
                        cboClassNames.Text = m_ExternalConnection.DatabaseClass;
                        txtConnectionString.Text = m_ExternalConnection.DatabaseConnectionString;

                        break;
                    case 1:
                        rdoOPCServer.Checked = true;
                        txtOpcMachineName.Text = m_ExternalConnection.OPCMachineName;
                        txtOPCServerName.Text = m_ExternalConnection.OPCServerName;


                        break;
                    case 2:
                        rdoQualityStormServer.Checked = true;
                        txtQualityStormServerName.Text = m_ExternalConnection.QualityStormServerName;
                        txtQualityStormPort.Text = m_ExternalConnection.QualityStormPort;
                        txtLogin.Text = m_ExternalConnection.QualityStormLogin;
                        txtPassword.Text = m_ExternalConnection.QualityStormLogin;

                        break;
                }
            } else {
                rdoDatabaseServer.Checked = true;
            }
        }

        public ConnectionContextBag ContextBag {
            get {
                return m_contextBag;
            }
            set {
                m_contextBag = value;
            }
        }

        public FrmExternalConnections(ConnectionContextBag bag) {
            m_contextBag = bag;
            InitializeComponent();
        }
        public FrmExternalConnections() {
            InitializeComponent();
        }

        private void rdoQualityStormServer_CheckedChanged(object sender, EventArgs e) {
            grpQualityStorm.Visible = rdoQualityStormServer.Checked;
        }

        private void rdoDatabaseServer_CheckedChanged(object sender, EventArgs e) {
            grpDatabase.Visible = rdoDatabaseServer.Checked;
        }

        private void rdoOPCServer_CheckedChanged(object sender, EventArgs e) {
            grpOPC.Visible = rdoOPCServer.Checked;
        }

        private void btnSearch_Click(object sender, EventArgs e) {
            try {
                Object o = ContextBag.Context.Service.GetOpcServers(txtOpcMachineName.Text);
                FrmSelectOPCServer frm = new FrmSelectOPCServer();
                frm.MachineName = txtOpcMachineName.Text;
                frm.Servers = o as Array;
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK) {
                    txtOPCServerName.Text = frm.ServerName;
                }
            } catch {
            }
        }

        private void FrmExternalConnections_Load(object sender, EventArgs e) {
            

            DataTable providers = m_contextBag.Context.Service.GetDatabaseProviders();
                        
        int i;
        for (i = 0; i < providers.Rows.Count; i++)
            cboClassNames.Items.Add(providers.Rows[i].ItemArray[2]);
            
        

        }

        private void txtOpcMachineName_TextChanged(object sender, EventArgs e) {

            btnSearch.Enabled = txtOpcMachineName.Text != "";
        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (txtConnectionName.Text == "") {
                MessageBox.Show("Please inform the name of the ExternalConnection.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            bool Adding = m_ExternalConnection == null;
            try {
                if (Adding) {
                    m_ExternalConnection = ExternalConnection.CreateExternalConnection(Guid.NewGuid());
                

                    if (m_contextBag.Folder != null) {
                        m_ExternalConnection.Parent = m_contextBag.Folder.Guid;
                    } else {
                        m_ExternalConnection.Parent = m_contextBag.Module.Guid;
                    }


                } 
                  
                m_ExternalConnection.Name = txtConnectionName.Text;
                if (rdoDatabaseServer.Checked) {
                    m_ExternalConnection.Type = 0;
                    m_ExternalConnection.DatabaseClass  =cboClassNames.Text;
                    m_ExternalConnection.DatabaseConnectionString = txtConnectionString.Text;
                }

                if (rdoOPCServer.Checked) {
                    m_ExternalConnection.Type = 1;
                    m_ExternalConnection.OPCMachineName = txtOpcMachineName.Text;
                    m_ExternalConnection.OPCServerName = txtOPCServerName.Text;

                }

                if (rdoQualityStormServer.Checked) {
                    m_ExternalConnection.Type = 2;
                    m_ExternalConnection.QualityStormServerName=txtQualityStormServerName.Text;
                    m_ExternalConnection.QualityStormPort=txtQualityStormPort.Text;
                    m_ExternalConnection.QualityStormLogin=txtLogin.Text;
                    m_ExternalConnection.QualityStormLogin=txtPassword.Text;

                }

                if (Adding) {
                    m_contextBag.Context.Service.AddExternalConnection(m_ExternalConnection);
                    m_contextBag.Context.Service.SaveChanges();
                } else {
                    m_contextBag.Context.Service.UpdateExternalConnection(m_ExternalConnection);
                }

                
                this.DialogResult = DialogResult.OK;
                Close();


            } catch (ObjectAlreadyCreatedException oace) {
                MessageBox.Show("There is already an External Connection with this name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void btnTest_Click(object sender, EventArgs e) {
            try {
                if (rdoDatabaseServer.Checked) {
                    if (m_contextBag.Context.Service.TestDatabaseConnection(cboClassNames.Text, txtConnectionString.Text)) {
                        MessageBox.Show("Database Connection is working!", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (rdoOPCServer.Checked) {
                    if (m_contextBag.Context.Service.TestOPCConnection(txtOpcMachineName.Text, txtOPCServerName.Text)) {
                        MessageBox.Show("OPC Connection is working!", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (rdoQualityStormServer.Checked) {
                    if (m_contextBag.Context.Service.TestQualityStormConnection(txtQualityStormServerName.Text, txtQualityStormPort.Text, txtLogin.Text, txtPassword.Text)) {
                        MessageBox.Show("QualityStorm Connection is working!", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            } catch (ExternalConnectionException ee) {
                MessageBox.Show("Error connecting! " + ee.Message, "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

       

        
    }
}
